const http = require('http')
const fs = require('fs')
const path = require('path')

const server = http.createServer()

server.on('request', (req, res) => {
    let url = req.url
    if (url === '/home') {
        res.writeHeader(200, { 'Content-Type': 'text/html;charset=utf-8' }) //设置响应头
        fs.readFile(path.join(__dirname, './static/home.html'), (err, data) => {
            if (err) {
                console.log(err)
            } else {
                //渲染页面
                res.write(data)
                res.end()
            }
        })
    }
    else if (url === '/login') {
        res.writeHeader(200, { 'Content-Type': 'text/html;charset=utf-8' }) //设置响应头
        fs.readFile(path.join(__dirname, './static/login.html'), (err, data) => {
            if (err) {
                console.log(err)
            } else {
                //渲染页面
                res.write(data)
                res.end()
            }
        })
    }
    else {
        res.writeHeader(200, { 'Content-Type': 'text/html;charset=utf-8' }) //设置响应头
        fs.readFile(path.join(__dirname, './static/404.html'), (err, data) => {
            if (err) {
                console.log(err)
            } else {
                //渲染页面
                res.write(data)
                res.end()
            }
        })
    }

})

server.listen(3000, () => {
    console.log('server is running at http://127.0.0.1:3000')
})